package util.www.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public abstract class ConexionSQLServer {
    
    //Connection con;
    
    
    //public static Connection getConexion(String host, String puerto, String nombreBD, String usuario, String clave) throws Exception {
    public static Connection getConexion(String urlConexion) throws Exception {
        Connection con = null;
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  //carga el driver
            //con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=enaho;user=sa;password=12345;");
            con = DriverManager.getConnection(urlConexion);
            //System.out.println("abriendo conexion");
        } catch (ClassNotFoundException ex) {
            //Logger.getLogger(ConexionBD.class.getName()).log(Level.SEVERE, null, ex);
            throw new Exception("Driver JDBC no encontrado",ex);
        } catch (SQLException ex) {
            //Logger.getLogger(ConexionBD.class.getName()).log(Level.SEVERE, null, ex);
            throw new Exception("No se pudo obtener una conexion a la Base de Datos",ex);
        }
        return con;
    }
    
    /*
    public static void operarConexion(Operacion op, String urlConexion) throws Exception {
        Connection con = null;
        try {
            con = getConexion(urlConexion);
            op.operar(con);
        } catch(Exception e) {
            if(con!=null)
                con.close();
        }
    } 
    */

    /*
    public ConexionSQLServer() throws Exception  {
        con = null;
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  //carga el driver
            con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=enaho;user=sa;password=12345;");
            System.out.println("abriendo conexion");
        } catch (ClassNotFoundException ex) {
            //Logger.getLogger(ConexionBD.class.getName()).log(Level.SEVERE, null, ex);
            throw new Exception("Driver JDBC no encontrado",ex);
        } catch (SQLException ex) {
            //Logger.getLogger(ConexionBD.class.getName()).log(Level.SEVERE, null, ex);
            throw new Exception("No se pudo obtener una conexion a la BD",ex);
        }
    }

    
    //solo es llamado por garbage collector
    @Override
    protected void finalize() throws Throwable {
        super.finalize(); 
        if(con!=null) {
            System.out.println("cerrando conexion");
            con.close();
        }
    }
    */    
    
    public static void testConexion() {
        try{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  // load the driver
            // line below needs to be modified to include the database name, user, and password (if any)
            Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=enaho;user=sa;password=12345;");

            System.out.println("Conectado a la base de datos !");
            
            con.close();
            
            System.out.println("Conexion cerrada");
        }
        catch(SQLException sqle) {
            System.out.println("Sql Exception :"+sqle.getMessage());
        }
        catch(ClassNotFoundException e) {
            System.out.println("Class Not Found Exception :" + e.getMessage());
        }    
    }


}
